package org.pwwong.fxscalautils.components.controls

import com.sun.scenario.scenegraph.fx.{FXGroup, FXShape,FXNode}

import java.awt.Color._
import java.awt.event.MouseEvent
import java.awt.event.InputEvent._

import org.pwwong.fxscalautils.components.containers.Stack
import org.pwwong.fxscalautils.components.layouthints._
import org.pwwong.fxscalautils.Predef._

import org.pwwong.scalautils.signals.Predef._

object Button {
  def apply() = new Button fillPaint(blue) border(5) 
}
class Button extends Stack {
  val Unpressed = CreateDefaultState('Unpressed) 
  val Hovered = CreateState('Hovered) 
  val Pressed = CreateState('Pressed)
  
  (Unpressed goto( (ev:(MouseEvent,Component)) => Some( if((ev._1.getModifiersEx & BUTTON1_DOWN_MASK) > 0) 
                                                          Pressed 
                                                        else 
                                                          Hovered ),
                   onMouseEntered) 
             goto(Pressed, onMousePressed))
  
  (Hovered   goto(Unpressed, onMouseExited) 
             goto(Pressed, onMousePressed))
  
  (Pressed   goto(Unpressed, onMouseExited) 
             goto(Hovered, onMouseReleased))
  
  protected val backgroundComp = RoundRectangle()
  protected val textComp = Text()
  
  val fillPaint  	= BoundedProperty(backgroundComp.fillPaint)
  val borderPaint 	= BoundedProperty(backgroundComp.borderPaint)
  val border 		= BoundedProperty(backgroundComp.border) 
  val cornerRadius 	= BoundedProperty(backgroundComp.cornerRadius)
  
  val text 			= BoundedProperty(textComp.text)
  val textFillPaint = BoundedProperty(textComp.fillPaint)
  val textVAlign	= BoundedProperty(textComp.y)
  val textHAlign	= BoundedProperty(textComp.x)
  val textFont		= BoundedProperty(textComp.font)
  
  setChildren( 
    backgroundComp cornerRadius(25.0) center(Center) middle(Middle) w(*) h(*), 
    textComp center(Center) bottom(Middle)
  )
}
